// 引入HTTP模块
const http = require('http');
// 引入express模块
const express = require('express');
const compression = require('compression');
const proxy = require('http-proxy-middleware');

const app = express();
const server = http.createServer(app);

app.use(compression());

app.use('/', express.static(__dirname + '/src')); // 指定静态HTML文件的位置
app.use('/', express.static(__dirname + '/dist')); // 指定静态HTML文件的位置
// const express = require('express');
// const proxy = require('http-proxy-middleware');

// var app = express();

// JSDOM.fromURL('https://account.teambition.com/login')
// .then(dom => {
//   const document = dom.window.document.getElementById('secrets');
//   const clientId = document.getAttribute('data-clientid');
//   const token = document.getAttribute('data-clienttoken');
// })
// .catch(err => {
//   console.log(err);
// });

// app.use('/', express.static(__dirname + '/src')); 
app.use('/', proxy({
  target: 'https://api.teambition.com',
  changeOrigin: true
}));

if (process.env.NODE_ENV !== 'production') {
  const webpack = require('webpack');
  const config = require('./webpack.dev.js');
  const webpackDevMiddleware = require('webpack-dev-middleware');
  const webpackHotMiddleware = require('webpack-hot-middleware');

  const compiler = webpack(config);
  app.use(webpackDevMiddleware(compiler, {
    stats: {
      colors: true,
      chunks: false
    },
    noInfo: true,
  }));

  app.use(webpackHotMiddleware(compiler));
}

// 监听3000端口
server.listen(process.env.PORT || 3000, (err) => {
  if (!err) console.log('server is running');
});
